/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AgregarModeradores.java
 *
 * Created on 07-sep-2011, 0:30:39
 */
package Ventanas;

import Objetos.PropiedadesIP;
import Proxies.CtrlAgregarModeradorWS;
import Proxies.CtrlAgregarModeradorWSService;
import Proxies.DataGrupoArray;
import Proxies.EnumRol;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.xml.namespace.QName;

/**
 *
 * @author Sebas
 */
public class AgregarModeradores extends javax.swing.JFrame {

    /** Creates new form AgregarModeradores */
    CtrlAgregarModeradorWS ctrlAgregarModerador;
    
    public AgregarModeradores() {
        
        initComponents();
        
        PropiedadesIP props = PropiedadesIP.getInstance();
        String IP = props.getServidorCentralIP();
        URL dirModerador = null;
        try {
            dirModerador = new URL(IP + "CtrlAgregarEntradaMuro?wsdl");
        } catch (MalformedURLException ex) {
            Logger.getLogger(AgregarModeradores.class.getName()).log(Level.SEVERE, null, ex);
        }
        QName qNaModerador = new QName("http://WebServices/", "CtrlAgregarEntradaMuroWSService");
        ctrlAgregarModerador = new CtrlAgregarModeradorWSService(dirModerador, qNaModerador).getCtrlAgregarModeradorWSPort();
        
        actualizarGrupos();
        actualizarParticipantes();
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane4 = new javax.swing.JScrollPane();
        JTable_buscarModerador = new javax.swing.JTable();
        JTextField_BuscarModerador = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        Jbtn_cancelar = new javax.swing.JButton();
        Jbtn_confirmar = new javax.swing.JButton();
        jComboBox_Grupos = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();

        setTitle("RED - Agregar moderadores");

        JTable_buscarModerador.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nick", "Email", "Nombre"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane4.setViewportView(JTable_buscarModerador);

        JTextField_BuscarModerador.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JTextField_BuscarModeradorActionPerformed(evt);
            }
        });
        JTextField_BuscarModerador.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                JTextField_BuscarModeradorPropertyChange(evt);
            }
        });
        JTextField_BuscarModerador.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                JTextField_BuscarModeradorKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                JTextField_BuscarModeradorKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                JTextField_BuscarModeradorKeyTyped(evt);
            }
        });

        jLabel9.setText("Participantes del grupo");

        Jbtn_cancelar.setText("Cancelar");
        Jbtn_cancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Jbtn_cancelarMouseClicked(evt);
            }
        });

        Jbtn_confirmar.setText("Confirmar");
        Jbtn_confirmar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Jbtn_confirmarMouseClicked(evt);
            }
        });

        jComboBox_Grupos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_GruposActionPerformed(evt);
            }
        });

        jLabel6.setText("Seleccionar grupo del sistema:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(jLabel6)
                        .addGap(72, 72, 72)
                        .addComponent(jComboBox_Grupos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(103, 103, 103)
                        .addComponent(jLabel9)
                        .addGap(75, 75, 75)
                        .addComponent(JTextField_BuscarModerador, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(103, 103, 103)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(250, 250, 250)
                        .addComponent(Jbtn_cancelar)
                        .addGap(26, 26, 26)
                        .addComponent(Jbtn_confirmar)))
                .addGap(82, 82, 82))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jComboBox_Grupos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel9))
                    .addComponent(JTextField_BuscarModerador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(61, 61, 61)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Jbtn_cancelar)
                    .addComponent(Jbtn_confirmar))
                .addContainerGap(38, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void JTextField_BuscarModeradorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JTextField_BuscarModeradorActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_JTextField_BuscarModeradorActionPerformed

    private void JTextField_BuscarModeradorPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_JTextField_BuscarModeradorPropertyChange
        // TODO add your handling code here:
}//GEN-LAST:event_JTextField_BuscarModeradorPropertyChange

    private void JTextField_BuscarModeradorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_JTextField_BuscarModeradorKeyPressed
        // TODO add your handling code here:
}//GEN-LAST:event_JTextField_BuscarModeradorKeyPressed

    private void JTextField_BuscarModeradorKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_JTextField_BuscarModeradorKeyReleased
        // TODO add your handling code here:
        TableModel modelo = JTable_buscarModerador.getModel();
        final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(modelo);
        JTable_buscarModerador.setRowSorter(sorter);
        String text = JTextField_BuscarModerador.getText();
        JTable_buscarModerador.setRowSelectionInterval(0, 0);
        if (text.length() == 0) {
            sorter.setRowFilter(null);
        } else {
            sorter.setRowFilter(javax.swing.RowFilter.regexFilter(text));
        }
}//GEN-LAST:event_JTextField_BuscarModeradorKeyReleased

    private void JTextField_BuscarModeradorKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_JTextField_BuscarModeradorKeyTyped
        // TODO add your handling code here:
}//GEN-LAST:event_JTextField_BuscarModeradorKeyTyped

    private void Jbtn_cancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_Jbtn_cancelarMouseClicked
        // TODO add your handling code here:
        this.setVisible(false);
}//GEN-LAST:event_Jbtn_cancelarMouseClicked

    private void Jbtn_confirmarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_Jbtn_confirmarMouseClicked
        // TODO add your handling code here:
        
        String nomGrupo, mailUsr;
        
        nomGrupo = jComboBox_Grupos.getSelectedItem().toString();
        mailUsr = JTable_buscarModerador.getModel().getValueAt(JTable_buscarModerador.getSelectedRow(), 1).toString();
        
        ctrlAgregarModerador.agregarModeradorWS(nomGrupo, mailUsr);
        
        JOptionPane.showMessageDialog(null, "Se cambio el rol del participante en el grupo", "Se agrego nuevo moderador al grupo", JOptionPane.INFORMATION_MESSAGE);

        actualizarParticipantes();
        
        
    }//GEN-LAST:event_Jbtn_confirmarMouseClicked

    private void jComboBox_GruposActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_GruposActionPerformed
        // TODO add your handling code here:
        actualizarParticipantes();
}//GEN-LAST:event_jComboBox_GruposActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AgregarModeradores().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable JTable_buscarModerador;
    private javax.swing.JTextField JTextField_BuscarModerador;
    private javax.swing.JButton Jbtn_cancelar;
    private javax.swing.JButton Jbtn_confirmar;
    private javax.swing.JComboBox jComboBox_Grupos;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane4;
    // End of variables declaration//GEN-END:variables

    private void actualizarParticipantes(){
        
        if (!ctrlAgregarModerador.listarGrupos().getItem().isEmpty()) {

            Iterator<Proxies.DataUsuario> itera = ctrlAgregarModerador.listarDusuariosGrupo(jComboBox_Grupos.getSelectedItem().toString()).getItem().iterator();
            Proxies.DataUsuario usr;
            Object[] fila = new Object[3];

            DefaultTableModel model = new DefaultTableModel();
            JTable_buscarModerador.setModel(model);
            model.addColumn("Nick");
            model.addColumn("Email");
            model.addColumn("Nombre");
            JTable_buscarModerador.setRowSelectionAllowed(true);
            JTable_buscarModerador.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

            while (itera.hasNext()) {
                usr = itera.next();
                if (usr.getRol() == EnumRol.PARTICIPANTE) {
                    fila[0] = usr.getNick();
                    fila[1] = usr.getCorreo();
                    fila[2] = usr.getNombre();
                    model.addRow(fila);
                }
            }

        }

        
    }
    
    private void actualizarGrupos() {

        DataGrupoArray grupos = ctrlAgregarModerador.listarGrupos();
        if (grupos.getItem().isEmpty()) {
            jComboBox_Grupos.addItem("No hay grupos en el sistema");
        } else {

            for (int i = 0; i < grupos.getItem().size(); i++) {
                jComboBox_Grupos.addItem(grupos.getItem().get(i).getNombre());
            }

        }

    }
}
